/*     */ package com.zimbra.cs.service.formatter;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.HttpUtil;
/*     */ import com.zimbra.cs.mailbox.Contact;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.service.UserServletContext;
/*     */ import com.zimbra.cs.service.UserServletException;
/*     */ import java.io.IOException;
/*     */ import java.nio.charset.Charset;
/*     */ import java.util.EnumSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import javax.servlet.ServletOutputStream;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class VcfFormatter
/*     */   extends Formatter
/*     */ {
/*     */   public FormatterFactory.FormatType getType()
/*     */   {
/*  43 */     return FormatterFactory.FormatType.VCF;
/*     */   }
/*     */   
/*     */   public String[] getDefaultMimeTypes()
/*     */   {
/*  48 */     return new String[] { "text/vcard", "text/x-vcard", "text/directory" };
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public Set<MailItem.Type> getDefaultSearchTypes()
/*     */   {
/*  57 */     return EnumSet.of(MailItem.Type.CONTACT);
/*     */   }
/*     */   
/*     */   public void formatCallback(UserServletContext context) throws IOException, ServiceException
/*     */   {
/*  62 */     Charset charset = context.getCharset();
/*  63 */     Iterator<? extends MailItem> iterator = null;
/*     */     try {
/*  65 */       iterator = getMailItems(context, getDefaultStartTime(), getDefaultEndTime(), 2147483647L);
/*     */       
/*  67 */       String filename = (context.target instanceof Contact) ? ((Contact)context.target).getFileAsString() : "contacts";
/*     */       
/*  69 */       String cd = HttpUtil.createContentDisposition(context.req, "attachment", filename + ".vcf");
/*  70 */       context.resp.addHeader("Content-Disposition", cd);
/*  71 */       context.resp.setContentType("text/x-vcard");
/*  72 */       context.resp.setCharacterEncoding(charset.name());
/*     */       
/*  74 */       int count = 0;
/*  75 */       while (iterator.hasNext()) {
/*  76 */         MailItem item = (MailItem)iterator.next();
/*  77 */         if ((item instanceof Contact))
/*     */         {
/*  79 */           VCard vcf = VCard.formatContact((Contact)item);
/*  80 */           context.resp.getOutputStream().write(vcf.getFormatted().getBytes(charset));
/*  81 */           count++;
/*     */         }
/*     */       }
/*  84 */     } finally { if ((iterator instanceof Formatter.QueryResultIterator)) {
/*  85 */         ((Formatter.QueryResultIterator)iterator).finished();
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public boolean supportsSave() {
/*  91 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */   public void saveCallback(UserServletContext context, String contentType, Folder folder, String filename)
/*     */     throws ServiceException, IOException, UserServletException
/*     */   {
/*  98 */     byte[] body = context.getPostBody();
/*  99 */     List<VCard> cards = VCard.parseVCard(new String(body, context.getCharset()));
/*     */     
/* 101 */     if ((cards == null) || (cards.size() == 0) || ((cards.size() == 1) && (((VCard)cards.get(0)).fields.isEmpty())))
/*     */     {
/* 103 */       throw new UserServletException(400, "no contact fields found in vcard");
/*     */     }
/*     */     
/*     */ 
/* 107 */     for (VCard vcf : cards) {
/* 108 */       if (!vcf.fields.isEmpty())
/*     */       {
/* 110 */         folder.getMailbox().createContact(context.opContext, vcf.asParsedContact(), folder.getId(), null);
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/formatter/VcfFormatter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */